package com.hcss.common.base;

import com.hcss.common.constant.CommonConstants;
import com.hcss.common.constant.ConfigConstants;
import com.hcss.paging.constant.PagingConstant;
import com.hcss.qywechat.user.model.vo.WQyUserVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by suvan on 2017/12/14.
 */
public class BaseClass {

    /**
     * 全局获取request
     * 主要用于非controller层获取request
     * @return HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 全局获取session
     * @return HttpSession
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * http:// 127.0.0.1/demo/index.html
     * @return 当前请求路径
     */
    public static String getPath() {
        return getRequest().getRequestURL().toString();
    }

    /**
     * http://127.0.0.1/demo/
     * @return 项目请求路径
     */
    public static String getProjectPath() {
        return getPath().replace(getRequest().getServletPath(), "") + "/";
    }

    /**
     * 获取session数据
     * @param key 要获取对象的key值
     * @return session中key对应的value值
     */
    public static Object getSessionAttribute(String key) {
        return getSession().getAttribute(key);
    }

    /**
     * 获取请求路径全链接
     * @return
     */
    public static String getFullPath() {
        HttpServletRequest request = getRequest();
        if (request.isSecure()){
            String port = (request.getServerPort() == 80)?"":":" + request.getServerPort();
            return request.getScheme() + "://" + request.getServerName() + port + request.getRequestURI();
        }
        return getRequest().getRequestURL().toString();
    }

    /**
     * 获取like查询用的参数，返回"%"+parameter+"%"
     * @param parameter
     * @return
     */
    public static String getQueryLikeParameter(String parameter) {
        return PagingConstant.LikeRegExp + parameter + PagingConstant.LikeRegExp;
    }

    /**
     * 带连接符的UUID
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 无“-”的UUID
     * @return
     */
    public static String getUUIDWithoutConnector() {
        return getUUID().replaceAll("-", "");
    }

    /**
     * 获取登录用户信息
     * @return
     */
    public static WQyUserVO getSessionUser() {
        Object object = SecurityUtils.getSubject().getPrincipal();
        if (object != null) {
            WQyUserVO user = (WQyUserVO) SecurityUtils.getSubject().getPrincipal();
            user.setPassword(null);
            return user;
        } else {
            return null;
        }
    }

    /**
     * 获取登录用户userid
     * @return
     */
    public static String getSessionUserid() {
        WQyUserVO user = getSessionUser();
        return user == null ? CommonConstants.EMPTY_STRING : user.getUserid();
    }

    /**
     * 获取登录用户拥有角色信息
     * @return
     */
    public static List<String> getSessionUserRole() {
        WQyUserVO user = getSessionUser();
        return (user == null || CollectionUtils.isEmpty(user.getRoles())) ? new ArrayList<String>(0) : user.getRoles();
    }

    /**
     * 是否是手机端请求
     * @return
     */
    public static boolean isMobileRequest() {
        HttpServletRequest request = getRequest();
        return request != null && (StringUtils.isNotBlank(request.getHeader(ConfigConstants.MOBILE_HEADER)));
    }
}
